package com.pentalog.rest.httpclient;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.pentalog.rest.Customer;
import com.pentalog.rest.json.Product;

/**
 * The Class ApacheHttpClientTest.
 */
public class ApacheHttpClientTest {
	
	  private static final Logger LOGGER = LoggerFactory.getLogger(ApacheHttpClientTest.class);

	/**
	 * The main method.
	 * 
	 * @param args
	 *            the arguments
	 */
	public static void main(String[] args) {
		testGetMethod();
		testPostMethod();
		testGetXMLWSReturnObject();
		testGetXMLWSReturnXML();
	}

	/**
	 * Test get xmlws return xml.
	 */
	private static void testGetXMLWSReturnXML() {

		String httpURLString = "http://localhost:8080/rest-example/rest/xml/customer/query?name=Tuong&pin=123";

		String fileName = "customer";
		try {
			File customerFile = RestClientWSUtil.callHTTPXMLWebService(
					fileName, httpURLString);
			LOGGER.info("File Name : {}",customerFile.getName());
			LOGGER.info("File content : {} ",customerFile.toString());
		} catch (Exception e) {
			LOGGER.error("Error : {}",e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * Test get xmlws return object.
	 */
	private static void testGetXMLWSReturnObject() {

		String httpURLString = "http://localhost:8080/rest-example/rest/xml/customer/query?name=Tuong&pin=123";

		try {
			Customer customer = RestClientWSUtil.callHTTPXMLWebService(
					Customer.class, httpURLString);
			LOGGER.info(customer.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Test post method.
	 */
	private static void testPostMethod() {
		try {

			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpPost postRequest = new HttpPost(
					"http://localhost:8080/rest-example/rest/json/product/post");

			StringEntity input = new StringEntity(
					"{\"qty\":100,\"name\":\"iPad 4\"}");
			input.setContentType("application/json");
			postRequest.setEntity(input);

			HttpResponse response = httpClient.execute(postRequest);

			if (response.getStatusLine().getStatusCode() != 201) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ response.getStatusLine().getStatusCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(response.getEntity().getContent())));

			String output;
			LOGGER.info("Output from Server .... \n");
			while ((output = br.readLine()) != null) {
				LOGGER.info(output);
			}

			httpClient.getConnectionManager().shutdown();

		} catch (MalformedURLException e) {

			e.printStackTrace();

		} catch (IOException e) {

			e.printStackTrace();

		}

	}

	/**
	 * Test get method.
	 */
	private static void testGetMethod() {
		try {
			Product product = RestClientWSUtil.callHTTPJSONWebService(
					Product.class,
					"http://localhost:8080/rest-example/rest/json/product/get");
			LOGGER.info("After converted to JSON object....");
			LOGGER.info(product.toString());

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}